﻿using Furion.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewLife.Messaging;
using NewLife.RocketMQ;
using Furion;
using furion.blog.Domain.Shared.Dto.RocketMQ;

namespace furion.blog.Application.RocketMQ.Producers
{
    /// <summary>
    /// 阿里云短信发送记录生产者
    /// </summary>
    public class AliSmsSendRecordProducer : ISingleton
    {
        Producer _mq = null;
        public static string TOPIC = "ALI-SMS-SEND-RECORD";
        string GROUP = "GROUP@ALI-SMS-SEND-RECORD";

        public AliSmsSendRecordProducer() {
            if (_mq == null) {
                _mq = new Producer
                {
                    //Topic = TOPIC ,
                    NameServerAddress = App.Configuration["RocketMQ:NameServer"],
                    //Log = XTrace.Log,
                    //Group = GROUP,
                };
            }
            _mq.Start();
            _mq.CreateTopic(TOPIC, 2);
        }

        public async Task Create(ProducerMsgDto dto) {

            //发送消息方式一，可以设置key
            NewLife.RocketMQ.Protocol.Message message = new NewLife.RocketMQ.Protocol.Message()
            {
                Body = dto.Body,
                Keys = dto.Keys,
                Tags = dto.Tags,
                Flag = dto.Flag,
                WaitStoreMsgOK = dto.WaitStoreMsgOK
            };
            //var sr = mq.Publish(message);
            var sr = await _mq.PublishAsync(message);
            Console.WriteLine(sr);

        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop() {
            if (this._mq != null) {
                this._mq.Dispose();
            }
            this._mq = null;
        }

    }
}
